package com.example.lib_net.interceptor

import okhttp3.Interceptor
import okhttp3.Response

/**
 * @author yaotianxue
 * @description 超时重连，默认重试一次，多次的话需要拦截器实现
 * @date 2024/10/24
 */
//超时重连拦截器
class RetryInterceptor(val maxRetry:Int): Interceptor {//maxRetry 最大重试次数
    private var  retryNum = 0 //已经重连多少次
    override fun intercept(chain: Interceptor.Chain): Response {
        val  request = chain.request()
        var response = chain.proceed(request)
        while (!response.isSuccessful && retryNum < maxRetry){
            retryNum++
            response = chain.proceed(request)
        }
        return response
    }
}
